package LopCoBan;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPasswordField;

import com.db4o.ObjectSet;


public class frmQuanLyNhanVien<MyTableModel> extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JButton btnThem;
	private JTextField txtMaSo;
	private JTextField txtHoTen;
	private JButton btnSua;
	private JButton btnXoa;
	private JButton btnBoQua;
	private JButton btnThoat;
	
	private String m_stMaNVHienTai = "";
	private int m_iDongHienTai = 0;
	private JComboBox cboChucVu;
	private JTextField txtNgaysinh;
	private JTextField txtDiachi;
	private JTextField txtEmail;
	private JPasswordField txtMatkhau;
	private JTextField txtLuongCB;
	private JTextField txtNgaylam;
	private JTextField txtNgaynghi;

	/**
	 * Create the frame.
	 */
	public frmQuanLyNhanVien() {
		initGUI();
		LoadCombobxo();
		LoadThongTinNV();
		DangKySuKienChonDong();
	}
	private void initGUI() {
		setTitle("Quản Lý Nhân Viên");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 676, 457);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Th\u00F4ng Tin:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 645, 148);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("M\u00E3 S\u1ED1:");
		lblNewLabel.setBounds(10, 32, 46, 14);
		panel.add(lblNewLabel);
		
		JLabel lblHTn = new JLabel("H\u1ECD T\u00EAn:");
		lblHTn.setBounds(154, 32, 46, 14);
		panel.add(lblHTn);
		
		JLabel lblChcV = new JLabel("Ch\u1EE9c v\u1EE5:");
		lblChcV.setBounds(408, 60, 60, 14);
		panel.add(lblChcV);
		
		txtMaSo = new JTextField();
		txtMaSo.setEnabled(false);
		txtMaSo.setBounds(66, 28, 60, 20);
		panel.add(txtMaSo);
		
		txtHoTen = new JTextField();
		txtHoTen.setEnabled(false);
		txtHoTen.setBounds(193, 29, 206, 20);
		panel.add(txtHoTen);
		
		cboChucVu = new JComboBox();
		cboChucVu.setEnabled(false);
		cboChucVu.setBounds(478, 57, 161, 20);
		panel.add(cboChucVu);
		
		JLabel lblNgySinh = new JLabel("Ngày sinh:");
		lblNgySinh.setBounds(408, 32, 60, 14);
		panel.add(lblNgySinh);
		
		txtNgaysinh = new JTextField();
		txtNgaysinh.setEnabled(false);
		txtNgaysinh.setBounds(478, 29, 161, 20);
		panel.add(txtNgaysinh);
		
		JComboBox cmbPhai = new JComboBox();
		cmbPhai.setModel(new DefaultComboBoxModel(new String[] {"Nam", "Nữ"}));
		cmbPhai.setSelectedIndex(2);
		cmbPhai.setEnabled(false);
		cmbPhai.setBounds(66, 57, 60, 20);
		panel.add(cmbPhai);
		
		JLabel lblGiiTnh = new JLabel("Giới tính:");
		lblGiiTnh.setBounds(10, 60, 46, 14);
		panel.add(lblGiiTnh);
		
		txtDiachi = new JTextField();
		txtDiachi.setEnabled(false);
		txtDiachi.setBounds(192, 57, 206, 20);
		panel.add(txtDiachi);
		
		JLabel lblaCh = new JLabel("Địa chỉ:");
		lblaCh.setBounds(154, 57, 46, 20);
		panel.add(lblaCh);
		
		JLabel lblinThoi = new JLabel("Mật khẩu:");
		lblinThoi.setBounds(10, 85, 54, 23);
		panel.add(lblinThoi);
		
		txtEmail = new JTextField();
		txtEmail.setEnabled(false);
		txtEmail.setBounds(193, 88, 206, 20);
		panel.add(txtEmail);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(154, 88, 40, 23);
		panel.add(lblEmail);
		
		txtMatkhau = new JPasswordField();
		txtMatkhau.setEditable(false);
		txtMatkhau.setBounds(66, 88, 60, 20);
		panel.add(txtMatkhau);
		
		txtLuongCB = new JTextField();
		txtLuongCB.setEnabled(false);
		txtLuongCB.setBounds(478, 88, 161, 20);
		panel.add(txtLuongCB);
		
		JLabel lblLngCBn = new JLabel("Lương CB:");
		lblLngCBn.setBounds(408, 91, 60, 14);
		panel.add(lblLngCBn);
		
		txtNgaylam = new JTextField();
		txtNgaylam.setEnabled(false);
		txtNgaylam.setBounds(86, 119, 112, 20);
		panel.add(txtNgaylam);
		
		JLabel lblNgyVoLm = new JLabel("Ngày vào làm:");
		lblNgyVoLm.setBounds(10, 119, 79, 20);
		panel.add(lblNgyVoLm);
		
		txtNgaynghi = new JTextField();
		txtNgaynghi.setEnabled(false);
		txtNgaynghi.setBounds(293, 119, 106, 20);
		panel.add(txtNgaynghi);
		
		JLabel lblNgyNghLm = new JLabel("Ngày nghỉ làm:");
		lblNgyNghLm.setBounds(217, 119, 79, 20);
		panel.add(lblNgyNghLm);
		
		JLabel lblQuyn = new JLabel("Quyền:");
		lblQuyn.setBounds(408, 122, 60, 14);
		panel.add(lblQuyn);
		
		JComboBox cmbQuyenHT = new JComboBox();
		cmbQuyenHT.setSelectedIndex(2);
		cmbQuyenHT.setEnabled(false);
		cmbQuyenHT.setBounds(478, 119, 161, 20);
		panel.add(cmbQuyenHT);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBounds(5, 353, 650, 55);
		contentPane.add(panel_1);
		
		btnThem = new JButton();
		btnThem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Them();
			}
		});
		btnThem.setText("Thêm");
		btnThem.setSize(new Dimension(50, 25));
		btnThem.setPreferredSize(new Dimension(50, 25));
		btnThem.setMnemonic(KeyEvent.VK_T);
		btnThem.setLocation(new Point(12, 10));
		btnThem.setBounds(50, 16, 69, 25);
		panel_1.add(btnThem);
		
		btnSua = new JButton();
		btnSua.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Sua();
			}
		});
		btnSua.setText("Sửa");
		btnSua.setSize(new Dimension(50, 25));
		btnSua.setMnemonic(KeyEvent.VK_S);
		btnSua.setLocation(new Point(97, 10));
		btnSua.setBounds(129, 16, 69, 25);
		panel_1.add(btnSua);
		
		btnXoa = new JButton();
		btnXoa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Xoa();
			}
		});
		btnXoa.setText("Xóa");
		btnXoa.setSize(new Dimension(80, 25));
		btnXoa.setMnemonic(KeyEvent.VK_X);
		btnXoa.setLocation(new Point(182, 10));
		btnXoa.setBounds(208, 16, 69, 25);
		panel_1.add(btnXoa);
		
		btnBoQua = new JButton();
		btnBoQua.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BoQua();
			}
		});
		btnBoQua.setText("Bỏ qua");
		btnBoQua.setSize(new Dimension(80, 25));
		btnBoQua.setMnemonic(KeyEvent.VK_B);
		btnBoQua.setLocation(new Point(267, 10));
		btnBoQua.setEnabled(false);
		btnBoQua.setBounds(287, 16, 69, 25);
		panel_1.add(btnBoQua);
		
		btnThoat = new JButton();
		btnThoat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Thoat();
			}
		});
		btnThoat.setText("Thoát");
		btnThoat.setSize(new Dimension(80, 25));
		btnThoat.setMnemonic(KeyEvent.VK_H);
		btnThoat.setLocation(new Point(390, 10));
		btnThoat.setBounds(366, 16, 69, 25);
		panel_1.add(btnThoat);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 170, 645, 172);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setBorder(null);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Mã số", "Họ tên", "Ngày sinh","Chức Vụ","Số Điện thoại","Ngày vào làm","Quyền HT"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				true, true, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(100);
		table.getColumnModel().getColumn(1).setPreferredWidth(115);
		table.getColumnModel().getColumn(2).setPreferredWidth(80);
		table.getColumnModel().getColumn(3).setPreferredWidth(80);
		table.getColumnModel().getColumn(4).setPreferredWidth(80);
		table.getColumnModel().getColumn(5).setPreferredWidth(80);
		table.getColumnModel().getColumn(6).setPreferredWidth(80);
		table.setBounds(234, 189, 203, -72);
		scrollPane.setViewportView(table);
		
	}
	
	
	private void LoadThongTinNV()
	{
		try{
			ObjectSet<NhanVien> dsNhanVien = CoSoDuLieu.LayDuLieu(NhanVien.class);			
			Vector heading = new Vector();
			heading.addElement("STT");
			heading.addElement("Mã số");
			heading.addElement("Họ Tên");
			heading.addElement("Ngày sinh");
			heading.addElement("Chức Vụ");
			heading.addElement("Số ĐT");
			heading.addElement("Ngày vào làm");
			heading.addElement("Quyền");
			
			Vector data = new Vector();
			int i = 1;
			for (NhanVien nv : dsNhanVien)
			{
				Vector rows = new Vector();				
				rows.addElement(i++);	//stt			
				rows.addElement(nv.getMaNV());
				rows.addElement(nv.getTenNV());
				rows.addElement(nv.getNgaySinh());
				rows.addElement(nv.getChucVu());
				rows.addElement(nv.getSoDT());
				rows.addElement(nv.getNgayVaoLam());
			//	rows.addElement(nv.getDiaChi());
			//	rows.addElement(nv.getNgayNghiLam());
				rows.addElement(nv.getQuyenHT());
				
				data.add(rows);		
			}
						
			//DefaultTableModel tempTable = new DefaultTableModel(data,heading);
			DefaultTableModel tempTable = new DefaultTableModel(data,heading);
			table.setModel (tempTable);
			if(data.size()>0){
				this.btnSua.setEnabled(true);
				this.btnXoa.setEnabled(true);
				this.m_stMaNVHienTai = ((Vector)data.elementAt(0)).elementAt(1).toString();
				this.txtMaSo.setText(m_stMaNVHienTai);
				this.txtHoTen.setText(((Vector)data.elementAt(0)).elementAt(2).toString());
				this.cboChucVu.setSelectedItem(((Vector)data.elementAt(0)).elementAt(3).toString());
				//this.txtChucVu.setText(((Vector)data.elementAt(0)).elementAt(3).toString());
			}else{
				this.btnSua.setEnabled(false);
				this.btnXoa.setEnabled(false);
				this.KhoiTaoNhap();
			}		
			table.getSelectionModel().setSelectionInterval(0, 0);
			//Canh chỉnh độ rộng cột
						
			table.getColumnModel().getColumn(0).setPreferredWidth(70);
			table.getColumnModel().getColumn(1).setPreferredWidth(115);
			table.getColumnModel().getColumn(2).setPreferredWidth(80);
			table.getColumnModel().getColumn(3).setPreferredWidth(80);
			table.getColumnModel().getColumn(4).setPreferredWidth(80);
			table.getColumnModel().getColumn(5).setPreferredWidth(80);
			table.getColumnModel().getColumn(6).setPreferredWidth(80);
			
		}catch(Exception e){
			e.printStackTrace ();			
		}
	}
	private void Them(){
		if(this.btnThem.getText().equals("Thêm")){
			this.m_iDongHienTai = this.table.getSelectedRow();
			this.btnThem.setText("Lưu");
			this.btnThem.setMnemonic(KeyEvent.VK_L);
			this.btnSua.setEnabled(false);
			this.btnXoa.setEnabled(false);
			this.CapNhatTrangThaiControls(true);
			this.KhoiTaoNhap();
			this.txtMaSo.requestFocus();
		}else{
			//kiểm tra dữ liệu nhập
			if(this.KiemTraDuLieuNhap()==false){
				return;
			}
			//kiểm tra trùng
			if(!CoSoDuLieu.KiemTraTrung(NhanVien.class, "m_stMaSo", this.txtMaSo.getText().trim()))
			{
				JOptionPane.showConfirmDialog(this, "Mã số này đã có rồi!", "Lỗi", JOptionPane.ERROR_MESSAGE);
				this.txtMaSo.requestFocus();
				this.txtMaSo.selectAll();
				return;
			}
			//lưu dữ liệu
			try{
				NhanVien nhanvien = new NhanVien();
				CoSoDuLieu.ThemDuLieu(nhanvien);
			}catch(Exception e){
				e.printStackTrace();
				JOptionPane.showConfirmDialog(this, "Đã có lỗi xảy ra khi thêm nhân viên!", "Lỗi", JOptionPane.ERROR_MESSAGE);
				return;
			}
			this.btnThem.setText("Thêm");
			this.btnThem.setMnemonic(KeyEvent.VK_T);
			this.btnSua.setEnabled(true);
			this.btnXoa.setEnabled(true);
			this.table.setEnabled(true);
			this.CapNhatTrangThaiControls(false);
			this.LoadThongTinNV();
		}
	}
	private void Sua(){
		if(this.btnSua.getText().equals("Sửa")){
			this.m_iDongHienTai = this.table.getSelectedRow();
			this.btnSua.setText("Lưu");
			this.btnSua.setMnemonic(KeyEvent.VK_L);
			this.btnThem.setEnabled(false);
			this.btnXoa.setEnabled(false);
			this.table.setEnabled(false);
			this.m_stMaNVHienTai = this.txtMaSo.getText().trim();			
			this.CapNhatTrangThaiControls(true);			
		}else{
			//kiểm tra dữ liệu nhập
			if(this.KiemTraDuLieuNhap()==false){
				return;
			}
			//kiểm tra trùng
			if(!this.txtMaSo.getText().trim().equals(this.m_stMaNVHienTai)){
				if(!CoSoDuLieu.KiemTraTrung(NhanVien.class, "m_stMaSo", this.txtMaSo.getText().trim()))
				{
					JOptionPane.showConfirmDialog(this, "Mã số này đã có rồi!", "Lỗi", JOptionPane.ERROR_MESSAGE);
					this.txtMaSo.requestFocus();
					this.txtMaSo.selectAll();
					return;
				}				
			}
			//lưu dữ liệu
			try{
				NhanVien nhanvien = new NhanVien();
				CoSoDuLieu.CapNhatDuLieu(NhanVien.class, "m_stMaSo", m_stMaNVHienTai, nhanvien);
			}catch(Exception e){
				e.printStackTrace();
				JOptionPane.showConfirmDialog(this, "Đã có lỗi xảy ra khi sữa nhân viên!", "Lỗi", JOptionPane.ERROR_MESSAGE);
				return;
			}
			this.btnSua.setText("Sửa");
			this.btnSua.setMnemonic(KeyEvent.VK_S);
			this.btnThem.setEnabled(true);
			this.btnXoa.setEnabled(true);
			this.table.setEnabled(true);
			this.CapNhatTrangThaiControls(false);
			this.LoadThongTinNV();
			this.table.setRowSelectionInterval(0, this.m_iDongHienTai);
		}
	}	
	private void Xoa()
	{
		if(this.m_stMaNVHienTai.equals("")){
			JOptionPane.showMessageDialog(this, "Chưa chọn nhân viên để xóa", "Lỗi", JOptionPane.OK_OPTION);
			return;
		}
		if(JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa mẫu tin này không?", "Xác Nhận",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION){
			try{
				CoSoDuLieu.XoaDuLieu(NhanVien.class, "m_stMaSo", m_stMaNVHienTai);
			}catch(Exception ex){
				ex.printStackTrace();
				JOptionPane.showMessageDialog(this, "Đã có lỗi khi xóa nhân viên", "Lỗi", JOptionPane.ERROR_MESSAGE);
				
			}			
			this.LoadThongTinNV();
		}
	}
	private void BoQua()
	{
		this.txtMaSo.setEnabled(false);
		this.txtHoTen.setEnabled(false);
		this.cboChucVu.setEnabled(false);
		this.btnThem.setEnabled(true);
		this.btnSua.setEnabled(true);
		this.btnXoa.setEnabled(true);
		this.btnThoat.setEnabled(true);
		this.btnThem.setText("Thêm");
		this.btnThem.setMnemonic(KeyEvent.VK_T);		
		this.btnSua.setText("Sửa");
		this.btnSua.setMnemonic(KeyEvent.VK_S);
		this.btnBoQua.setEnabled(false);
		this.table.setEnabled(true);		
		this.LoadThongTinNV();	
		this.table.setRowSelectionInterval(0, this.m_iDongHienTai);
	}
	private void Thoat()
	{
		this.dispose();
	}
	
	private void DangKySuKienChonDong()
	{
		ListSelectionModel rowSM = this.table.getSelectionModel();
//		dang ky su kien ListSelectionEvent
        rowSM.addListSelectionListener(
        	//khoi tao listener thong qua adapter
            new ListSelectionListener() 
            {
                public void valueChanged(ListSelectionEvent e) {
                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    
                    if (lsm.isSelectionEmpty()){
                        m_stMaNVHienTai = "";
                    } 
                    else 
                    {	try{
                    		int selectedRow = lsm.getMinSelectionIndex();
//                			lay gia tri cua cot 0 tai dong duoc chon
                    		m_stMaNVHienTai = table.getModel().getValueAt(selectedRow, 1).toString();
                    		txtMaSo.setText(m_stMaNVHienTai);
                    		txtHoTen.setText(table.getModel().getValueAt(selectedRow, 2).toString());
                    		cboChucVu.setSelectedItem(table.getModel().getValueAt(selectedRow, 3).toString());                    		
                    		                   		          	
                    	}catch(Exception ex){
                    		//ex.printStackTrace();
                    	}                   	
                                                 
                    }
                }
            }
        );
	}
	
	private boolean KiemTraDuLieuNhap()
	{
		if(this.txtMaSo.getText().trim().equals("")){
			JOptionPane.showConfirmDialog(this, "Chưa Nhập Mã Số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
			this.txtMaSo.requestFocus();
			return false;
		}
		if(this.txtHoTen.getText().trim().equals("")){
			JOptionPane.showConfirmDialog(this, "Chưa nhập họ tên!", "Lỗi", JOptionPane.ERROR_MESSAGE);			
			this.txtHoTen.requestFocus();
			return false;			
		}
//		if(this.txtChucVu.getText().trim().equals("")){
//			JOptionPane.showConfirmDialog(this, "Chưa nhập chức vụ!", "Lỗi", JOptionPane.ERROR_MESSAGE);			
//			this.txtChucVu.requestFocus();
//			return false;			
//		}
		return true;
	}
	
	private void CapNhatTrangThaiControls(boolean edited){
		this.txtMaSo.setEnabled(edited);
		this.txtHoTen.setEnabled(edited);
		this.cboChucVu.setEnabled(edited);
		this.btnBoQua.setEnabled(edited);
		this.btnThoat.setEnabled(!edited);
	}
	private void KhoiTaoNhap()
	{
		m_stMaNVHienTai = "";
		cboChucVu.setSelectedIndex(2);
		txtMaSo.setText("");
		txtHoTen.setText("");	
		this.txtDiachi.setText("");
		this.txtEmail.setText("");
		this.txtLuongCB.setText("");
		this.txtMatkhau.setText("");
		this.txtNgaysinh.setText("");
		this.txtNgaylam.setText("");
		this.txtNgaynghi.setText("");
		
	}
	private void LoadCombobxo()
	{
		//ChucVu cv = new ChucVu();
		ObjectSet<ChucVu> dscv = CoSoDuLieu.LayDuLieu(ChucVu.class);
		for( ChucVu cv:dscv)
		{
			Object[] r = { cv.getMaChucVu(), cv.getTenChucVu()};
			cboChucVu.addItem(r);
		}
		
		this.cboChucVu.setSelectedIndex(2);
		
	
	}
}
